#ifndef __WIZ_BASE_CAST_LEXICAL_HPP__SHANHAOBO_19800429__
#define __WIZ_BASE_CAST_LEXICAL_HPP__SHANHAOBO_19800429__

#include "../../../Config/WizConfig.hpp"
#include "../TypeTraits/WizBaseTypeTraitsIsSame.hpp"
#include "../TypeTraits/WizBaseTypeTraitsIf.hpp"
#include "../TypeTraits/WizBaseTypeTraitsCharType.hpp"

namespace Wiz
{
    namespace Cast
    {
        //////////////////////////////////////
        template<typename CharT1, typename CharT2>
        struct ChoiseCharType
        {
            typedef char tType;
        };

        template<typename CharT1>
        struct ChoiseCharType<CharT1, wchar_t>
        {
            typedef wchar_t tType;
        };
        template<typename CharT2>
        struct ChoiseCharType<wchar_t, CharT2>
        {
            typedef wchar_t tType;
        };

        template<typename T1, typename T2>
        struct ChoiseWCharStrStream
        {
            typedef typename ::Wiz::TypeTraits::If_
                <
                    ::Wiz::TypeTraits::Is::Same
                    <
                        typename ChoiseCharType
                        < 
                            typename ::Wiz::TypeTraits::CharType<T1>::tType,
                            typename ::Wiz::TypeTraits::CharType<T2>::tType 
                        >::tType,
                        ::std::wstringstream::char_type
                    >,
                    ::std::wstringstream,
                    ::std::stringstream
                >::tType tType;
        };
        ////////////////////////////////////////////////////////////////////////
        template<class RetT, class ArgT>
        WIZ_INLINE RetT Lexical(ArgT Src)
        {
            typename ChoiseWCharStrStream<RetT, ArgT>::tType tInterpreter;
            RetT Result;
            if(tInterpreter << Src)
            {
                if (tInterpreter >> Result)
                {
                    return Result;
                }
            }
            throw ::std::bad_cast("::Wiz::Cast::Lexical");
            return Result;
        }
        ////////////////////////////////////////////////////////////////////////
    } /// end of namespace Cast
} /// end of namespace Wiz

#define WIZ_DEFCAST_LEXICAL(T, Dest, Src)		T Dest = ::Wiz::Cast::Lexical<T>(Src)

#endif /*__WIZ_BASE_CAST_LEXICAL_HPP__SHANHAOBO_19800429__*/
